package com.demo.module.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.module.entity.SzyUserBehavior;
import com.demo.module.mapper.SzyUserBehaviorMapper;
import com.demo.module.model.InteractionActiveUserModel;
import com.demo.module.model.InteractionColumnClickModel;
import com.demo.module.model.InteractionStockEvaluationModel;
import com.demo.module.param.JeecgBootException;
import com.demo.module.param.UserBehaviorEnum;
import com.demo.module.param.UserBehaviorSaveParams;
import com.demo.module.service.ISzyUserBehaviorService;
import com.demo.module.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SzyUserBehaviorServiceImpl extends ServiceImpl<SzyUserBehaviorMapper, SzyUserBehavior> implements ISzyUserBehaviorService {


    @Autowired
    private SzyUserBehaviorMapper behaviorMapper;

    @Override
    @Transactional(rollbackFor = JeecgBootException.class)
    public Result<?> saveBehavior(UserBehaviorSaveParams params) {
        if(!getAliasExists(params.getName())){
            log.error("未找到埋点事件 params：{"+ JSON.toJSONString(params)+"}");
            return Result.OK();
        }
        SzyUserBehavior userBehavior = new SzyUserBehavior();
        userBehavior.setUserId(params.getUserId());
        userBehavior.setType(getAliasType(params.getName()));
        userBehavior.setName(getAliasName(params.getName()));
        Integer activityValue = getAliasActivityValue(params.getName());
        userBehavior.setActivityStatus(activityValue == -1?0:1);
        userBehavior.setActivityValue(activityValue);
        userBehavior.setCreateTime(new Date());
        int isSave = behaviorMapper.insert(userBehavior);
        if(isSave <= 0) {
            log.error("保存埋点出错 params：{"+JSON.toJSONString(userBehavior)+"}");
            return Result.OK();
        }
        return Result.OK();
    }

    @Override
    public InteractionColumnClickModel getColumnClick(String startTime, String endTime) {
        return behaviorMapper.getColumnClick(startTime,endTime);
    }

    @Override
    public InteractionStockEvaluationModel getStockEvaluation(String startTime, String endTime) {
        return behaviorMapper.getStockEvaluation(startTime,endTime);
    }

    @Override
    public List<InteractionActiveUserModel> getActiveUser(String startTime, String endTime) {
        return behaviorMapper.getActiveUser(startTime,endTime);
    }

    @Override
    public Integer getContentStatistics(String startTime, String endTime) {
        return behaviorMapper.getContentStatistics(startTime, endTime);
    }

    @Override
    public Integer getWeekContentStatistics() {
        return behaviorMapper.getWeekContentStatistics();
    }

//    public Integer saveActivityValue(String userId, Integer activityValue){
//        return baseAPI.addUserActivityValue(userId, activityValue);
//    }
    public Boolean getAliasExists(String alias) {
        for (UserBehaviorEnum userBehaviorEnum : UserBehaviorEnum.values()) {
            if (userBehaviorEnum.getAlias().equals(alias)) {
                return true;
            }
        }
        return false;
    }
    public Integer getAliasActivityValue(String alias) {
        for (UserBehaviorEnum userBehaviorEnum : UserBehaviorEnum.values()) {
            if (userBehaviorEnum.getAlias().equals(alias)) {
                return userBehaviorEnum.getActivityValue();
            }
        }
        return null;
    }


    public String getAliasName(String alias) {
        for (UserBehaviorEnum userBehaviorEnum : UserBehaviorEnum.values()) {
            if (userBehaviorEnum.getAlias().equals(alias)) {
                return userBehaviorEnum.getName();
            }
        }
        return null;
    }

    public Integer getAliasType(String alias) {
        for (UserBehaviorEnum userBehaviorEnum : UserBehaviorEnum.values()) {
            if (userBehaviorEnum.getAlias().equals(alias)) {
                return userBehaviorEnum.getType();
            }
        }
        return null;
    }
}
